package eg10_5;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class main {
    public static void main(String[] args) {
        File helloFile = new File("F://", "HelloWorld.txt");
        try {
            // 创建文件
            boolean isCreated = helloFile.createNewFile();
            if (isCreated || helloFile.exists()) {
                // 以对象流方式写入文件helloFile
                Student mike = new Student("Mike", 23, 21);
                try (FileOutputStream fileOut = new FileOutputStream(helloFile);
                     ObjectOutputStream objectOut = new ObjectOutputStream(fileOut)) {
                    objectOut.writeObject(mike);
                } // try-with-resources会自动关闭资源
                // 以对象流方式从文件helloFile读取Mike
                try (FileInputStream fileIn = new FileInputStream(helloFile);
                     ObjectInputStream objectIn = new ObjectInputStream(fileIn)) {
                    Student stuMike = (Student) objectIn.readObject();
                    System.out.println("Name: " + stuMike.getName());
                    System.out.println("Num: " + stuMike.getNum());
                    System.out.println("Age: " + stuMike.getAge());
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}